package com.sjz.myfrist;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

/***
 * ListView基本用法 一列展示图片和文字
 * 使用的是ArrayAdapter
 */
public class ListView2 extends AppCompatActivity {

    private List<Image> list = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.e("ListView2","1111111");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.csdn7_2);
        init();//初始化数据
        Log.e("ListView2长度",list.size()+"");
        Adapter ad = new Adapter(ListView2.this,R.layout.item,list);
        ListView listview = findViewById(R.id.list_view2);
        listview.setAdapter(ad);

        //点击某一行 触发事件
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Image image = list.get(i);
                Toast.makeText(ListView2.this, image.getName(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void init(){
        for(int i = 0; i < 5; i++){
            Image a = new Image("A",R.mipmap.ic_launcher);
            list.add(a);
            Image b = new Image("B",R.mipmap.ic_launcher);
            list.add(b);
            Image c = new Image("C",R.mipmap.ic_launcher);
            list.add(c);
            Image d = new Image("D",R.mipmap.ic_launcher);
            list.add(d);
            Image e = new Image("E",R.mipmap.ic_launcher);
            list.add(e);
        }
    }
}
